﻿using Estimate.Common.DBFieldTypes;
using Estimate.Common.Estimate;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;

namespace DataShow.trend
{
    /// <summary>
    /// Handler 的摘要说明
    /// </summary>
    public class Handler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string Date = context.Request.QueryString["date"];
            string Station = context.Request.QueryString["station"];
            string Scene = context.Request.QueryString["scene"];
            string SendPoint = context.Request.QueryString["sendpoint"];
            string Product = context.Request.QueryString["product"]; //Ec_thinFore|欧洲中心,xxxx|xxxx
            string[] Products = Product.Split(',');
            codeModel.TrendResponseModel model = new codeModel.TrendResponseModel() { };
            for (int i = 1; i < Convert.ToInt32(Scene)+1; i++)        //增加category
            {
                model.Category.Add(DateTime.Parse(Date).AddDays(i).ToString("yyyy-MM-dd"));
            }
            if (!string.IsNullOrEmpty(Product))
            {
                foreach (string product in Products)
                {
                    codeModel.TrendModel maxt = new codeBLL.TrendBLL().GetResponseModel(DateTime.Parse(Date), Station, Scene, SendPoint, product.Replace("'", ""), "maxt");
                    codeModel.TrendModel mint = new codeBLL.TrendBLL().GetResponseModel(DateTime.Parse(Date), Station, Scene, SendPoint, product.Replace("'", ""), "mint");
                    model.Max.Add(maxt);
                    model.Min.Add(mint);
                }
            }
            codeModel.TrendModel realmax = new codeModel.TrendModel() { name="高温实况" };
            codeModel.TrendModel realmin = new codeModel.TrendModel() { name="低温实况" };
            TempForeRealCompare comparerMax = null,comparerMin=null;
            DateTime dt = DateTime.Parse(Date);

            for (int i = 1; i < Convert.ToInt32(Scene)+1; i++)
            {
                comparerMax = new Estimate.Common.Estimate.ForeRealLinker().GetForeRelatedTempCondition(DateTime.Parse(Date), ConvertForeSpanType(Scene), ConvertPoint(SendPoint), Estimate.Common.DBFieldTypes.ForeFactorType.maxt);
                string type = comparerMax.TempType.ToString();
                string sql = string.Format("select * from AutoTempReal where [date]={0} and [type]='{1}'", dt.AddDays(i).ToString("yyyyMMdd"), type);
                DataTable table = new codeDAL.SqlHelper().ExecuteQuery(sql, CommandType.Text);
                string value = "";
                if (table.Rows.Count > 0)
                {
                    value = new codeBLL.TrendBLL().FilterStation(table.Rows[0]["value"].ToString(), Station);
                    realmax.data.Add(Convert.ToDouble(value));
                }
                comparerMin = new Estimate.Common.Estimate.ForeRealLinker().GetForeRelatedTempCondition(DateTime.Parse(Date), ConvertForeSpanType(Scene), ConvertPoint(SendPoint), Estimate.Common.DBFieldTypes.ForeFactorType.mint);
                type = comparerMin.TempType.ToString();
                sql = string.Format("select * from AutoTempReal where [date]={0} and [type]='{1}'", dt.AddDays(i).ToString("yyyyMMdd"), type);
                table = new codeDAL.SqlHelper().ExecuteQuery(sql, CommandType.Text);
                if (table.Rows.Count > 0)
                {
                    value = new codeBLL.TrendBLL().FilterStation(table.Rows[0]["value"].ToString(), Station);
                    realmin.data.Add(Convert.ToDouble(value));
                }
            }
            model.Max.Add(realmax);
            model.Min.Add(realmin);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            StringBuilder sb = new StringBuilder();
            jss.Serialize(model, sb);
            context.Response.Write(sb.ToString());
        }

        //时效转换
        private Estimate.Common.DBFieldTypes.ForeSpanType ConvertForeSpanType(string scene)
        {
            if (scene == "3")
            {
                return Estimate.Common.DBFieldTypes.ForeSpanType.FD48_72;
            }
            else
            {
                return Estimate.Common.DBFieldTypes.ForeSpanType.FD96_120;
            }
        }
        //转换起报时刻
        private Estimate.Common.DBFieldTypes.ForeClockType ConvertPoint(string sendpoint)
        {
            if (sendpoint == "H06")
            {
                return Estimate.Common.DBFieldTypes.ForeClockType.H08;
            }
            else if (sendpoint == "H10")
            {
                return Estimate.Common.DBFieldTypes.ForeClockType.H08;
            }
            else
            {
                return Estimate.Common.DBFieldTypes.ForeClockType.H20;
            }
        }

        


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}